package ru.hh.school;

import java.io.*;

public class ClientInputHandler extends Thread {
    private final ObjectOutputStream clientEndpointOut;
    private final ObjectInputStream localEndpointIn;
    private final Thread clientChatConsoleThread;

    public ClientInputHandler(OutputStream clientEndpointOut, InputStream localEndpointIn, Thread clientChatConsoleThread) throws IOException {
        this.clientEndpointOut = new ObjectOutputStream(clientEndpointOut);
        this.localEndpointIn = new ObjectInputStream(localEndpointIn);
        this.clientChatConsoleThread = clientChatConsoleThread;
    }

    public void run() {
        try {
            while (!Thread.interrupted()) {
                clientEndpointOut.writeObject(localEndpointIn.readObject());
            }
        } catch (Exception e) {
                // Ignore

        } finally {
            if (clientEndpointOut != null) {
                try {
                    clientEndpointOut.close();
                } catch (IOException e1) {
                    // Ignore
                }
            }
            if (localEndpointIn != null) {
                try {
                    localEndpointIn.close();
                } catch (IOException e1) {
                    // Ignore
                }
            }
            clientChatConsoleThread.interrupt();
        }
    }
}
